<template>
  <view class="container">
    <!-- 顶部标题和模式切换 -->
    <view class="header">
      <text class="title">外卖美食盲盒</text>
      <text class="subtitle">开启你的随机美食惊喜</text>
      
      <!-- 模式切换 -->
      <view class="mode-switch">
        <text class="mode-label">模式选择：</text>
        <button 
          class="mode-btn" 
          :class="{ active: currentMode === 'classic' }"
          @click="switchMode('classic')"
        >
          经典模式
        </button>
        <button 
          class="mode-btn" 
          :class="{ active: currentMode === 'simple' }"
          @click="switchMode('simple')"
        >
          简洁模式
        </button>
      </view>
    </view>

    <!-- 盲盒展示区域 -->
    <view class="box-container">
      <view class="blind-box" :class="{ shaking: isShaking }">
        <image 
          class="box-image" 
          :src="currentBoxImage" 
          mode="aspectFill"
        ></image>
        
        <!-- 美食结果展示 -->
        <view class="food-result" v-if="showFoodResult">
          <image 
            class="food-image" 
            :src="selectedFood.image" 
            mode="aspectFill"
          ></image>
          <text class="food-name">{{ selectedFood.name }}</text>
          <text class="food-price">¥{{ selectedFood.price }}</text>
          <text class="food-desc">{{ selectedFood.description }}</text>
          
          <!-- 摇晃时的遮罩层 -->
          <view class="shake-mask" v-if="isShaking && showFoodResult"></view>
        </view>
        
        <!-- 初始盲盒盖子 -->
        <view class="box-lid" v-if="!showFoodResult"></view>
      </view>
    </view>

    <!-- 操作按钮 -->
    <view class="button-group">
      <button 
        class="shake-btn" 
        @click="openBlindBox" 
        :disabled="isShaking"
      >
        {{ showFoodResult ? '摇一摇' : '开启盲盒' }}
      </button>
      
      <button 
        class="checkout-btn" 
        @click="goToCheckout" 
        v-if="showFoodResult && !isShaking"
      >
        去结算
      </button>
    </view>

    <!-- 历史记录 -->
    <view class="history-section" v-if="historyList.length > 0">
      <text class="history-title">历史记录</text>
      <scroll-view class="history-list" scroll-x>
        <view class="history-item" v-for="(item, index) in historyList" :key="index">
          <image class="history-image" :src="item.image" mode="aspectFill"></image>
          <text class="history-name">{{ item.name }}</text>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 当前模式
      currentMode: 'classic', // classic:经典模式, simple:简洁模式
      
      // 盲盒状态
      isShaking: false,
      showFoodResult: false,
      
      // 盲盒图片
      currentBoxImage: '/static/blind-box.png',
      
      // 美食数据
      foodList: [
        {
          name: '香辣小龙虾',
          price: 88,
          image: '/static/food1.jpg',
          description: '精选洪湖小龙虾，秘制香辣配方，麻辣鲜香'
        },
        {
          name: '招牌披萨',
          price: 68,
          image: '/static/food2.jpg',
          description: '意式手工披萨，芝士浓郁，配料丰富'
        },
        {
          name: '日式肥牛饭',
          price: 38,
          image: '/static/food3.jpg',
          description: '精选澳洲肥牛，搭配秘制酱汁，口感醇厚'
        },
        {
          name: '川味麻辣烫',
          price: 42,
          image: '/static/food4.jpg',
          description: '正宗川味汤底，食材新鲜，麻辣过瘾'
        },
        {
          name: '粤式叉烧饭',
          price: 36,
          image: '/static/food5.jpg',
          description: '蜜汁叉烧，肥瘦相间，口感香甜'
        },
        {
          name: '韩式炸鸡',
          price: 58,
          image: '/static/food6.jpg',
          description: '外酥里嫩，甜辣可口，配啤酒绝配'
        }
      ],
      
      // 当前选中的美食
      selectedFood: {},
      
      // 历史记录
      historyList: []
    };
  },
  methods: {
    // 切换模式
    switchMode(mode) {
      this.currentMode = mode;
    },
    
    // 开启/摇晃盲盒
    openBlindBox() {
      if (this.isShaking) return;
      
      // 经典模式：有动画效果
      if (this.currentMode === 'classic') {
        // 开始摇晃动画
        this.isShaking = true;
        
        // 模拟摇晃过程
        setTimeout(() => {
          this.isShaking = false;
          this.generateRandomFood();
        }, 2000);
      } 
      // 简洁模式：立即显示结果
      else {
        this.generateRandomFood();
      }
    },
    
    // 生成随机美食
    generateRandomFood() {
      // 随机选择美食
      const randomIndex = Math.floor(Math.random() * this.foodList.length);
      this.selectedFood = this.foodList[randomIndex];
      
      // 添加到历史记录
      this.historyList.unshift(this.selectedFood);
      if (this.historyList.length > 5) {
        this.historyList.pop();
      }
      
      this.showFoodResult = true;
      
      // 震动反馈
      uni.vibrateShort();
    },
    
    // 去结算
    goToCheckout() {
      uni.showToast({
        title: `已选择${this.selectedFood.name}，去结算！`,
        icon: 'success'
      });
      
      // 这里可以添加跳转到结算页面的逻辑
      // uni.navigateTo({
      //   url: '/pages/checkout/checkout?food=' + JSON.stringify(this.selectedFood)
      // });
    }
  }
};
</script>

<style scoped>
.container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20rpx;
}

.header {
  text-align: center;
  padding: 40rpx 0;
}

.title {
  font-size: 48rpx;
  font-weight: bold;
  color: #333;
  display: block;
  margin-bottom: 10rpx;
}

.subtitle {
  font-size: 28rpx;
  color: #666;
  display: block;
  margin-bottom: 30rpx;
}

/* 模式切换样式 */
.mode-switch {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 15rpx;
  margin-top: 20rpx;
}

.mode-label {
  font-size: 28rpx;
  color: #666;
}

.mode-btn {
  padding: 10rpx 20rpx;
  border-radius: 20rpx;
  font-size: 26rpx;
  background: #eee;
  color: #666;
  border: none;
}

.mode-btn.active {
  background: #ff4d4f;
  color: white;
}

.box-container {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40rpx 0;
}

.blind-box {
  width: 500rpx;
  height: 600rpx;
  position: relative;
  border-radius: 20rpx;
  overflow: hidden;
  box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.2);
}

.shaking {
  animation: shake 0.3s infinite;
}

@keyframes shake {
  0% { transform: rotate(-3deg) translateX(-5rpx); }
  50% { transform: rotate(3deg) translateX(5rpx); }
  100% { transform: rotate(-3deg) translateX(-5rpx); }
}

.box-image {
  width: 100%;
  height: 100%;
}

/* 统一的遮罩颜色样式 */
.box-lid, .shake-mask {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #4d4d4d; /* 不透明灰色，视觉效果匹配 rgba(0,0,0,0.7) */
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 10;
}

.box-lid::after {
  content: '外卖盲盒';
  color: white;
  font-size: 40rpx;
  font-weight: bold;
}

.shake-mask::after {
  content: '摇晃中...';
  color: white;
  font-size: 36rpx;
  font-weight: bold;
  animation: pulse 1s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

.food-result {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20rpx;
  box-sizing: border-box;
  z-index: 5;
}

.food-image {
  width: 400rpx;
  height: 300rpx;
  border-radius: 10rpx;
  margin-bottom: 20rpx;
}

.food-name {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 10rpx;
}

.food-price {
  font-size: 32rpx;
  color: #ff4d4f;
  margin-bottom: 10rpx;
}

.food-desc {
  font-size: 26rpx;
  color: #666;
  text-align: center;
  line-height: 1.5;
}

.button-group {
  display: flex;
  justify-content: center;
  gap: 20rpx;
  padding: 20rpx 0;
}

.shake-btn, .checkout-btn {
  padding: 20rpx 40rpx;
  border-radius: 50rpx;
  font-size: 32rpx;
  border: none;
}

.shake-btn {
  background: #ff4d4f;
  color: white;
}

.checkout-btn {
  background: #48bb78;
  color: white;
}

.history-section {
  margin-top: 40rpx;
}

.history-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 20rpx;
  display: block;
}

.history-list {
  display: flex;
  gap: 20rpx;
  padding-bottom: 20rpx;
}

.history-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 150rpx;
}

.history-image {
  width: 120rpx;
  height: 120rpx;
  border-radius: 10rpx;
  margin-bottom: 10rpx;
}

.history-name {
  font-size: 24rpx;
  color: #666;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}
</style>